import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {
    List,
} from './DTO';
const baseUrl = '/afterSales/api/v1';
const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '服务满意度调查问题维护单'
});

const listDesc = formatMessage({
    id: 'api.message.listDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const detailDesc = formatMessage({
    id: 'api.message.detailDesc',
    defaultMessage: '{resourceName}详情'
}, {resourceName});

const addDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '新增{resourceName}'
}, {resourceName});

const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '编辑{resourceName}'
}, {resourceName});

const abandonDesc = formatMessage({
    id: 'api.message.abandonDesc',
    defaultMessage: '作废{resourceName}'
}, {resourceName});
const effectiveDesc = formatMessage({
    id: 'api.message.effectiveDesc',
    defaultMessage: '生效{resourceName}'
}, {resourceName});


// 查询
export const getList = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/serviceSatisfactionSurveies/forCurrentUser?${queryString}`, null, List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, listDesc));
};

// 新增
export const addInfo = data => request(`${baseUrl}/serviceSatisfactionSurveies`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));

// 编辑
export const updateInfo = (id, data) => request(`${baseUrl}/serviceSatisfactionSurveies/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));

// 详情
export const getDetail = id =>
    request(`${baseUrl}/serviceSatisfactionSurveies/${id}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, detailDesc));
// 作废
export const abandonInfo = (id, data) => request(`${baseUrl}/serviceSatisfactionSurveies/${id}/abandon`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDesc));
// 生效
export const effectiveInfo = (id, data) => request(`${baseUrl}/serviceSatisfactionSurveies/${id}/effective`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, effectiveDesc));
